﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace TimeTravel.Extentions
{
    /// <summary>
    /// 记录集
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [DataContract]
    public class RecordSet<T> : IEnumerable<T>
    {
        [DataMember(Name = "records")]
        private readonly List<T> records = new List<T>();

        /// <summary>
        /// 获取指定索引项
        /// </summary>
        /// <param name="index">纪录存储索引</param>
        /// <returns></returns>
        public T this[int index]
        {
            get
            {
                return records[index];
            }
        }

        /// <summary>
        /// 页面开始索引
        /// </summary>
        [DataMember(Name = "pageIndex")]
        public int PageIndex { get; set; }

        /// <summary>
        /// 页面分页大小
        /// </summary>
        [DataMember(Name = "pageSize")]
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        [DataMember(Name = "totalRecords")]
        public int TotalRecords { get; set; }

        /// <summary>
        /// 记录集
        /// </summary>
        public IEnumerable<T> Records
        {
            get
            {
                return records;
            }
        }

        /// <summary>
        /// 是否有返回值
        /// </summary>
        public bool HasResults
        {
            get
            {
                return (records.Count > 0) ? true : false;
            }
        }

        /// <summary>
        /// 移除匹配项
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(T item)
        {
            return records.Remove(item);
        }

        /// <summary>
        /// 添加项
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            records.Add(item);
        }

        /// <summary>
        /// 添加集合
        /// </summary>
        /// <param name="collects"></param>
        public void AddRange(IEnumerable<T> collects)
        {
            records.AddRange(collects);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<T> GetEnumerator()
        {
            return records.GetEnumerator();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
